export const state = () => ({
    queryData: {},
    SUCCESS: 200,
    LOGIN_FLAG: false,
    loginInfo: {
        loginId: "",
        loginName: "",
    },
    params: [],
    buttons: [],
    menus: {
        data: [],
        siderCollapsed: false,
        current: "",
        title: "",
        subTitle: "",
        searchData: [],
        cache: [],
    },
    forms: {
        see: false,
        action: ""
    }
})

export const mutations = {
    current(state, v) {
        state.menus.current = v;
    },
    siderCollapsed(state) {
        state.menus.siderCollapsed = !state.menus.siderCollapsed;
    },
    title(state, v) {
        state.menus.title = v;
    },
    subTitle(state, v) {
        state.menus.subTitle = v;
    },
    params(state, v) {
        state.params = v;
    },
    buttons(state, v) {
        state.buttons = v;
    },
    LOGIN_FLAG(state, v) {
        state.LOGIN_FLAG = v;
    },
    see(state, v) {
        state.forms.see = v;
    },
    action(state, v) {
        state.forms.action = v;
    },
    loginInfo(state, v) {
        state.loginInfo = v;
    },
    menuData(state, v) {
        state.menus.data = v;
    },
    searchData(state, v) {
        state.menus.searchData = v;
    },
    queryData(state, v) {
        state.queryData[state.menus.current] = v;
    },
    menuCache(state, v) {
        if (!state.menus.cache) {
            state.menus.cache = new Array();
        }
        let idx = state.menus.cache.findIndex(item => item.url == v.url);
        if (idx == -1) {
            state.menus.cache.push(v);
        }
    }
}

export const actions = {
    async loadData({ state }, data) {
        let res = await this.$axios.put(state.menus.current, data);
        return res.data;
    },
    async getFormData({ state }, id) {
        let res = await this.$axios.get(state.menus.current + "/" + id);
        return res.data.data;
    },
    addEvent({ state }, { _this, data }) {
        this.$axios.post(state.menus.current, data).then(res => {
            let data = res.data;
            _this.StaticFunc.dyAlert(state.SUCCESS, _this, data)
        });
    },
    editEvent({ state }, { _this, data }) {
        this.$axios.put(state.menus.current + "/edit", data).then(res => {
            let data = res.data;
            _this.StaticFunc.dyAlert(state.SUCCESS, _this, data)
        });
    },
    deleteEvent({ state }, { _this, id }) {
        _this.$Confirm('确定删除该信息？', "删除").then(() => {
            this.$axios.delete(state.menus.current + "/" + id).then(res => {
                let data = res.data;
                _this.StaticFunc.dyAlert(state.SUCCESS, _this, data, false);
            });
        });
    },
    download({ state }, { url, data }) {
        this.$axios({
            method: "POST",
            timeout: 60 * 1000 * 10,
            url: url,
            data: data,
            responseType: "blob"
        }).then(res => {
            let blob = new Blob([res.data], {
                type: "application/vnd.ms-excel;charset=utf-8"
            });
            let contentDisposition = res.headers["content-disposition"];
            let patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
            let result = patt.exec(contentDisposition);
            let filename = decodeURIComponent(result[1]);
            let downloadElement = document.createElement("a");
            let href = window.URL.createObjectURL(blob);
            downloadElement.style.display = "none";
            downloadElement.href = href;
            downloadElement.download = filename;
            document.body.appendChild(downloadElement);
            downloadElement.click();
            document.body.removeChild(downloadElement);
            window.URL.revokeObjectURL(href);
        });
    },
    getParams({ state }, groupId) {
        return state.params[groupId];
    }
}